﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Business;
public partial class admin_TestFaultsMng : System.Web.UI.Page
{
    DataTable dt_result;
    DataTable dt_formtable;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadFaults();
        else
        {
           
          //    dt_result =(DataTable)  Session["dt"];
           //   GridView1.DataSource = dt_result;
          //  GridView1.DataBind();
        }
    }
    	    protected void PageIndexChanging(object sender, GridViewPageEventArgs e) 
	    { 
        //指定新页面，重新绑定数据 
	       GridView1.PageIndex=e.NewPageIndex;
           LoadFaults();
           GridView1.DataBind();
    }
            protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //Set the edit index.
                GridView1.EditIndex = e.NewEditIndex;
                //Bind data to the GridView control.
                LoadFaults();
                //GridView1.DataBind();
            }
            protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                //Reset the edit index.
                GridView1.EditIndex = -1;
                //Bind data to the GridView control.
                GridView1.DataBind();

               // BindData();
            }

            protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                //Retrieve the table from the session object.
                //DataTable dt = (DataTable)Session["dt"];
                //){
                //}
                //Update the values.
                GridViewRow row = GridView1.Rows[e.RowIndex];
                string faulttitle = ((TextBox)(row.Cells[0].Controls[0])).Text;
                string faultdes = ((TextBox)(row.Cells[1].Controls[0])).Text;
                DropDownList severity = (DropDownList)row.Cells[2].FindControl("severity_dropdownlist");
               int status_id = Int32.Parse(severity.SelectedValue);
                string recom = ((TextBox)(row.Cells[3].Controls[0])).Text;
                int count = row.Cells.Count;
            int recom_id= Int32.Parse(((TextBox)(row.Cells[5].Controls[0])).Text);
            //   Label lb = (Label) row.Cells[6].Controls[0];
                int fault_id = Int32.Parse(((TextBox)(row.Cells[6].Controls[0])).Text);
                string query = "Update FaultsCondition set faultName='"+faulttitle+"',faultDes='"
                    +faultdes+"' WHERE "
                    + "fault_id='" + fault_id + "'";
                  
              
                CreateConn objcon = new CreateConn();
                objcon.SQLConnDataTable(query);
                CreateConn objcon_reco = new CreateConn();
                string updateReco_query = "Update FaultRecommendation set status_id='"+status_id+"', recommendation='" + recom + "' WHERE "
                   + "recommend_id='" + recom_id + "'";
                objcon_reco.SQLConnDataTable(updateReco_query);
                ////Reset the edit index.
                GridView1.EditIndex = -1;
                LoadFaults();
               // GridView1.DataBind();
                ////Bind data to the GridView control.
                //BindData();
            }
    protected void LoadFaults()
    {
        string query = "SELECT faultName, faultDes,status_id,fc.fault_id ,recommendation,recommend_id"
            + " From FaultsCondition fc join FaultRecommendation fr on fc.fault_id"
            + "=fr.fault_id and fc.faultType!=0";
        CreateConn objconn = new CreateConn();
        try
        {
            dt_result = objconn.SQLConnDataTable(query);
        
            if (dt_result != null && dt_result.Rows.Count > 0)
            {
                //FormView1.DataSource = dt_result;
                // return;
            
                dt_result.Columns.Add("severity");
                foreach (DataRow dr in dt_result.Rows)
                {
                    string status = dr["status_id"].ToString();
                    dr.BeginEdit();
                    try
                    {

                        dr["severity"] = this.getsev(Int32.Parse(status));//change the value
                    }
                    catch(Exception e){
                        Response.Write("Err:"+e.ToString());
                    }
                  //  dt_result.Rows.
                         dr.EndEdit();
                    //  dr2.
                }
                //result_Enumerator = dt_result.Rows.GetEnumerator();

                // total_page = dt_result.Rows.Count / 10 + 1;
                //Session["TotalPage"] = total_page;
                Session["dt"] = dt_result;

                GridView1.DataSource = dt_result;
                GridView1.DataBind();

            }
           
        }
        catch (Exception e)
        {
            string errmsg = e.ToString();
        }

    }
    protected string getsev(int statusid)
    {
        string str = "";
        switch(statusid){
            case 1: str = "NORMAL"; break;
            case 2: str = "ABNORMAL"; break;
            case 3: str = "Moderate"; break;
            case 4: str = "Severe"; break;
            default: str = ""; break;
        }
        return str;
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
        int faultID = Int32.Parse(DropDownList2.SelectedValue);
        
        int statusID = Int32.Parse(DropDownList3.SelectedValue);
        string recom = TextBox_recom.Text;
       
        string query = "INSERT INTO FaultRecommendation(fault_id,status_id,recommendation)"
            +"values('"+faultID+"','"+statusID+"','"+recom+"')";
        CreateConn objconn = new CreateConn();
        try
        {
            objconn.SQLConnDataTable(query);
            this.LoadFaults();
        }
        catch (Exception e2)
        {
            Response.Write(e2.ToString());
        }
    }
}
